package com.rocketmq.demo.base.producer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: rocketmq-demo
 * @Package: com.rocketmq.demo.base.producer
 * @ClassName: OneWayProducer
 * @Author: mac
 * @Description:发送单向消息
 * @Date: 2021/4/1 11:17 上午
 * @Version: 1.0
 */
public class OneWayProducer {

    public static void main(String[] args) {
        //1.创建消息生产者,并制定组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");

        //2.设置nameService 地址
        producer.setNamesrvAddr("192.168.31.67:9876;192.168.31.126:9876;192.168.31.209:9876");


        try {
            for (int i = 0; i < 3; i++) {
                //3.启动producer
                producer.start();
                // 参数1:消息主题Topic 参数2:消息tag  参数3:消息类容
                Message msg = new Message("base", "Tag3", ("hello world" + 1).getBytes());

                //发送单体消息
                producer.sendOneway(msg);

                TimeUnit.SECONDS.sleep(1);
            }
        } catch (MQClientException | RemotingException | InterruptedException e) {
            e.printStackTrace();
        }finally {
            //关闭生产者
            producer.shutdown();
        }
    }
}
